﻿//#define V4
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Web.SessionState;

[WebService ( Namespace = "http://tempuri.org/" )]
[WebServiceBinding ( ConformsTo = WsiProfiles.BasicProfile1_1 )]
[ScriptService]
public class repeater_product : System.Web.Services.WebService
{

	[WebMethod]
	[ScriptMethod]
#if V4
	public object GetProductList ( int pageindex, int pagesize, string productname, string model, float price1, float price2, short amount, DateTime manufactureDate1, DateTime manufactureDate2 )
#else
	public SortedDictionary<string, object> GetProductList ( int pageindex, int pagesize, string productname, string model, float price1, float price2, short amount, DateTime manufactureDate1, DateTime manufactureDate2 )
#endif
	{
		this.Context.Response.Cache.SetNoStore ( );

		int beginIndex = pagesize * ( pageindex - 1 ) + 1;
		int endIndex = pagesize * pageindex;

		string where = string.Empty;

		if ( !string.IsNullOrEmpty ( productname ) )
			where += string.Format ( " and (ProductName like '{0}%')", productname );

		if ( !string.IsNullOrEmpty ( model ) )
			where += string.Format ( " and (Model like '{0}%')", model );

		if ( price1 != -1 )
			where += string.Format ( " and (Price >= {0})", price1 );

		if ( price2 != -1 )
			where += string.Format ( " and (Price <= {0})", price2 );

		if ( amount != -1 )
			where += string.Format ( " and (Amount = {0})", amount );

		if ( manufactureDate1.ToString ( "yyyy-M-d" ) != "1970-1-1" )
			where += string.Format ( " and (ManufactureDate >= #{0}#)", manufactureDate1.ToString ( "yyyy-M-d" ) );

		if ( manufactureDate2.ToString ( "yyyy-M-d" ) != "1970-1-1" )
			where += string.Format ( " and (ManufactureDate <= #{0}#)", manufactureDate2.ToString ( "yyyy-M-d" ) );

		int itemcount = SampleHelper.QueryValue ( string.Format ( "select count(*) from product where (1=1){0}", where ) );
		DataTable productTable = SampleHelper.ExecuteQuery ( string.Format ( "select * from (select *, (select count(*) from book where ID <= X.ID{2}) as RN from product as X where (1=1){2} order by ID asc) as Y where RN >= {0} and RN <= {1}", beginIndex, endIndex, where ), "Product" ).Product;

#if V4
		List<object> rows = new List<object> ( );

		foreach ( SampleDS.ProductRow row in productTable.Rows )
			rows.Add ( new { id = row["id"], productname = row["productname"], model = row["model"], price = row["price"], amount = row["amount"], manufacturedate = row["manufacturedate"] } );

		return new
		{
			__success = true,
			rows = rows.ToArray ( ),
			itemcount = itemcount
		};
#else
		List<object> tableJSON = SampleHelper.CreateJSONArray ( productTable );

		return SampleHelper.CreateJSONObject ( new KeyValuePair<string, object>[]
			{
			new KeyValuePair<string, object> ( "__success", true ),
			new KeyValuePair<string, object> ( "rows", tableJSON ),
			new KeyValuePair<string, object> ( "itemcount", itemcount )
			}
			);
#endif

		/*
		 * {
		 * "__success": true,
		 * "rows":
		 * [
		 * { "id": 0, "bookname": "xxx", ... }
		 * ],
		 * "itemcount": 100
		 * }
		 * */
	}

}
